package com.trace.pplos.parse.dao.dataobject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trace.pplos.parse.common.ParseEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class WifiInfo {
    private static final Logger logger = LoggerFactory.getLogger(WifiInfo.class);

    private Integer id;

    private String bssid;

    private String ssid;

    private String levels;

    private String gtype;

    private Integer gid;

    public WifiInfo(Integer id, String bssid, String ssid, String levels, String gtype, Integer gid) {
        this.id = id;
        this.bssid = bssid;
        this.ssid = ssid;
        this.levels = levels;
        this.gtype = gtype;
        this.gid = gid;
    }

    public WifiInfo() {
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBssid() {
        return bssid;
    }

    public void setBssid(String bssid) {
        this.bssid = bssid == null ? null : bssid.trim();
    }

    public String getSsid() {
        return ssid;
    }

    public void setSsid(String ssid) {
        this.ssid = ssid == null ? null : ssid.trim();
    }

    public String getLevels() {
        return levels;
    }

    public void setLevels(String levels) {
        this.levels = levels == null ? null : levels.trim();
    }

    public String getGtype() {
        return gtype;
    }

    public void setGtype(String gtype) {
        this.gtype = gtype;
    }

    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public static List<WifiInfo> toBean(String object, Integer parentId, ParseEnum parseEnum){
        logger.info("object:{},parentId:{},parseEnum:{}", object, parentId, parseEnum.getRemark());
        List<WifiInfo> wifiSsidList = new ArrayList<>();
        if(parentId != null && parentId.intValue()>0){
            try{
                JSONObject record = JSON.parseObject(object);
                JSONArray list = record.getJSONArray("wifiList");
                logger.info("wifiList:{}", list);
                for (int i = 0; i < list.size(); i++) {
                    JSONObject dataObj = list.getJSONObject(i);
                    WifiInfo info = new WifiInfo();
                    info.setSsid(record.getString("SSID"));
                    info.setBssid(record.getString("BSSID"));
                    info.setLevels(record.getString("level"));
                    info.setGtype(parseEnum.getCode());
                    info.setGid(parentId);
                    wifiSsidList.add(info);
                }
            }catch (Exception e){
                logger.error(e.getMessage(), e);
            }
        }else{
            logger.error("WifiInfo toBean parentId is null!");
        }
        return wifiSsidList;
    }

}